﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamBuilder.DictionaryAccess;

namespace DreamBuilder.UI.UserControls.DataAccess.Services
{
    public abstract class UChildCommon
    {
        public static string GetChildTablePKField(string childEditID)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childEditID);
            TableDicInfo pTableDicInfo = DictionaryAccessFacade.GetTableDicInfo(pChildEditTotalInfo.TableCode);
            return pTableDicInfo.PrimaryField;
        }

        public static string GetChildTableFKField(string childEditID)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childEditID);
            TableDicInfo pTableDicInfo = DictionaryAccessFacade.GetTableDicInfo(pChildEditTotalInfo.TableCode);
            if (string.IsNullOrEmpty(pTableDicInfo.ParentConnectField))
            {
                throw new Exception(pTableDicInfo.TableCode + "'s ParentConnectField is null in Configuration_Sys_Table");
            }
            return pTableDicInfo.ParentConnectField;
        }
        public static string GetBusySelectField(string childEditID)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childEditID);
            if (string.IsNullOrEmpty(pChildEditTotalInfo.BusySelectField))
            {
                throw new Exception(childEditID + "'s BusySelectField is null in Configuration_Data_ChildEditTotal");
            }
            return pChildEditTotalInfo.BusySelectField;
        }

        public static int GetBusySelectType(string childEditID)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childEditID);
            return (int)pChildEditTotalInfo.BusySelectType;
        }

        public static string GetChildTableCode(string childEditID)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childEditID);
            return pChildEditTotalInfo.TableCode;
        }

        /// <summary>
        /// 根据子表的项信息找出参照表代码
        /// </summary>
        /// <param name="pChildItem">子表的项信息</param>
        /// <returns></returns>
        public static string GetChildReferenceObj(ChildTableItemInfo pChildItem)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(pChildItem.ChildEditID);
            FieldDicInfo pFieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(pChildEditTotalInfo.TableCode, pChildItem.FieldCode);
            if (string.IsNullOrEmpty(pFieldDicInfo.ReferenceObj))
            {
                throw new Exception("TableCode:" + pFieldDicInfo.TableCode + " and FieldCode:" + pFieldDicInfo.FieldCode + "'s vReferenceObj is null in Configuration_Sys_Field");
            }
            return pFieldDicInfo.ReferenceObj;
        }

        /// <summary>
        /// 根据子表的项信息找出表代码
        /// </summary>
        /// <param name="pChildItem">子表的项信息</param>
        /// <returns></returns>
        public static string GetChildTableCode(ChildTableItemInfo pChildItem)
        {
            ChildEditTotalInfo pChildEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(pChildItem.ChildEditID);
            return pChildEditTotalInfo.TableCode;
        }


        public static string GetRefTableCode(string childEditID)
        {
            ChildEditTotalInfo pChildTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childEditID);
            if (string.IsNullOrEmpty(pChildTotalInfo.BusySelectField))
            {
                throw new Exception(pChildTotalInfo.ChildEditID + "'s BusySelectField is null in Configuration_Data_ChildEditTotal");
            }
            FieldDicInfo pFieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(pChildTotalInfo.TableCode, pChildTotalInfo.BusySelectField);
            if (string.IsNullOrEmpty(pFieldDicInfo.ReferenceObj))
            {
                throw new Exception("TableCode:" + pFieldDicInfo.TableCode + " and FieldCode:" + pFieldDicInfo.FieldCode + "'s vReferenceObj is null in Configuration_Sys_Field");
            }
            return pFieldDicInfo.ReferenceObj;
        }
    }
}
